/**
 * 
 */
package com.alkandros.myloke.adapters;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.alkandros.myloke.R;
import com.alkandros.myloke.models.NearByModel;

/**
 * @author Jiju Induchoodan
 *
 */
public class PlacesAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<NearByModel> nearbyModelArray;
	private LayoutInflater inflater;
	
	public PlacesAdapter(Context context,ArrayList<NearByModel> array)
	{
		this.context  			= context;
		this.nearbyModelArray   = array;
		inflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		return nearbyModelArray.size();
	}
	@Override
	public Object getItem(int arg0) {
		return nearbyModelArray.get(arg0);
	}
	@Override
	public long getItemId(int arg0) {
		return 0;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		View rowView = convertView;
		final NearByModel model  = (NearByModel) this.getItem(position);
		if (rowView == null)
		{
			ViewHolder viewHolder 			= new ViewHolder();
			rowView 						= inflater.inflate(R.layout.list_item, null);
			
			viewHolder.txtName				=(TextView)rowView.findViewById(R.id.textViewName);
			viewHolder.txtDistance			=(TextView)rowView.findViewById(R.id.textViewDistance);
			rowView.setTag(viewHolder);
		} 
		 ViewHolder holder = (ViewHolder) rowView.getTag();
		 
		// holder.txtDistance.setText(Float.valueOf(model.getDistance()).toString()+" m");
		 holder.txtDistance.setText(new DecimalFormat("##.#").format(Float.parseFloat(model.getDistance()))+" m");
		 holder.txtName.setText(model.getName());
	
	return rowView;
	}
	static class ViewHolder {
	    TextView txtName;
	    TextView txtDistance;
	}

}
